package com.sgb.packers.viewmodel;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.lifecycle.MutableLiveData;

import com.sgb.link.api.CommonNet;
import com.sgb.packers.api.PackersNet;
import com.sgb.packers.entity.ContractLaborEntity;
import com.sgb.packers.entity.PointEntity;
import com.sgb.packers.entity.WorkRecordsEntity;
import com.swgk.core.base.api.ICallBack;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.common.base.TeamViewModel;
import com.swgk.core.interfaces.IView;
import com.swgk.core.util.PickSelectEntity;

import org.jetbrains.annotations.NotNull;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * @author : younghow
 * @date : 2021/11/18 11:28
 * description:
 */
public class JobWorkEditFragViewModel extends TeamViewModel<IView> {

    public MutableLiveData<Boolean> uploadSuccess = new MutableLiveData<>();
    public MutableLiveData<List<String>> uploadUrls = new MutableLiveData<>();
    public MutableLiveData<List<PickSelectEntity>> projectUnitEntity = new MutableLiveData<>();
    public MutableLiveData<ContractLaborEntity> jobWorkDetail = new MutableLiveData<>();
    public MutableLiveData<Boolean> delJobWork = new MutableLiveData<>();

    public JobWorkEditFragViewModel(@NonNull @NotNull Application application) {
        super(application);
    }

    /**
     * 获取工程量单位
     * 此处未调用后端接口，由移动端写死
     * 平方米, 立方米, 吨, 米, 寸, 个, 次, 天, 块, 组, 台, 捆, 宗, 项, 株
     */
    public void getProjectUnit() {
        List<PickSelectEntity> projectUnitEntityList = new ArrayList<>();
        projectUnitEntityList.add(new PickSelectEntity("1", "平方米"));
        projectUnitEntityList.add(new PickSelectEntity("2", "立方米"));
        projectUnitEntityList.add(new PickSelectEntity("3", "吨"));
        projectUnitEntityList.add(new PickSelectEntity("4", "米"));
        projectUnitEntityList.add(new PickSelectEntity("5", "寸"));
        projectUnitEntityList.add(new PickSelectEntity("6", "个"));
        projectUnitEntityList.add(new PickSelectEntity("7", "次"));
        projectUnitEntityList.add(new PickSelectEntity("8", "天"));
        projectUnitEntityList.add(new PickSelectEntity("9", "块"));
        projectUnitEntityList.add(new PickSelectEntity("10", "组"));
        projectUnitEntityList.add(new PickSelectEntity("11", "台"));
        projectUnitEntityList.add(new PickSelectEntity("12", "捆"));
        projectUnitEntityList.add(new PickSelectEntity("13", "宗"));
        projectUnitEntityList.add(new PickSelectEntity("14", "项"));
        projectUnitEntityList.add(new PickSelectEntity("15", "株"));
        projectUnitEntity.setValue(projectUnitEntityList);
    }

    public void uploadWork(Map<String, Object> map) {
        iView.loading();
        PackersNet.getInstance().getService().uploadDayWork(map).enqueue(new ICallBack<BaseEntity>() {
            @Override
            public void onSuccess(BaseEntity data) {
                uploadSuccess.postValue(true);
            }
        });
    }

    public void uploadFile(List<File> files) {
        iView.loading();
        Map<String, RequestBody> photos = new HashMap<>(10);
        int i = 0;
        for (File file : files) {
            RequestBody requestBody = RequestBody.create(MediaType.parse("multipart/form-data"), file);
            photos.put("files" + i + "\"; filename=\"" + i + "image.jpg\"", requestBody);
            i++;
        }
        CommonNet.getInstance().getService().uploadMutiFileDrive(photos).enqueue(new ICallBack<BaseEntity<List<String>>>() {
            @Override
            public void onSuccess(BaseEntity<List<String>> data) {
                if (data.isSuccess()) {
                    uploadUrls.postValue(data.getData());
                }
            }
        });
    }

    public void wrapWorkDetail(String no,String type) {
        iView.loading();
        PackersNet.getInstance().getService().wrapWorkDetail(no,type).enqueue(new ICallBack<BaseEntity<ContractLaborEntity>>() {
            @Override
            public void onSuccess(BaseEntity<ContractLaborEntity> data) {
                jobWorkDetail.postValue(data.getData());
            }
        });
    }

    public void delJobWork(String no) {
        iView.loading();
        PackersNet.getInstance().getService().delJobWork(no).enqueue(new ICallBack<BaseEntity>() {
            @Override
            public void onSuccess(BaseEntity data) {
                delJobWork.postValue(true);
            }
        });
    }
}
